package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.ApUserDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ApUserAuthServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserAuthService {

    /**
     * 分页查询用户
     */
    public ResponseResult listUser(ApUserDto dto){
        QueryWrapper<ApUserRealname> wrapper=new QueryWrapper<>();
        if (dto.getStatus()!=null){
            wrapper.eq("status",dto.getStatus());
        }
        if (dto.getId()!=null){
            wrapper.eq("id",dto.getId());
        }

        IPage<ApUserRealname> page=new Page<ApUserRealname>(dto.getPage(),dto.getSize());
        page = page(page, wrapper);
        if (dto.getPage()>page.getPages()){
            IPage<ApUserRealname> page2=new Page<>(page.getPages(),dto.getSize());
            page=page(page2,wrapper);
        }

        ResponseResult result=new PageResponseResult((int)page.getCurrent(),(int)page.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    /**
     * 审核失败
     * @param dto
     * @return
     */
    public ResponseResult authFail(ApUserDto dto){
        if (dto.getId()==null){
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        ApUserRealname apUserRealname=new ApUserRealname();
        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus((short) 2);
        apUserRealname.setReason(dto.getMsg());
        boolean b = updateById(apUserRealname);
        if (!b){
            throw new CustomException(AppHttpCodeEnum.FAIL_TO_UPDATE_USERREALNAME);
        }
        return new ResponseResult<>();
    }

    /**
     * 审核通过
     * @param dto
     * @return
     */
    public ResponseResult authPass(ApUserDto dto){
        if (dto.getId()==null){
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        ApUserRealname apUserRealname=new ApUserRealname();
        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus((short) 9);
        boolean b = updateById(apUserRealname);
        if (!b){
            throw new CustomException(AppHttpCodeEnum.FAIL_TO_UPDATE_USERREALNAME);
        }
        return new ResponseResult<>();
    }
}
